package android;

import hibernate.Driverstab;
import hibernate.DriverstabDAO;


import java.io.IOException;
import java.io.OutputStream;
import java.util.List;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.json.JSONException;
import org.json.JSONObject;



/**
 * Servlet implementation class LoginServlet
 */

public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doPost(request, response);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	@SuppressWarnings("unchecked")
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("utf-8");
		
		
		JSONObject jsonObj=null;
		String reciveAccount=null;
		String recivePwd=null;
		try {
			 jsonObj=new JSONObject(request.getParameter("jsonString"));
			 reciveAccount=jsonObj.getString("account");
			 recivePwd=jsonObj.getString("pwd");
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}	
		
		
		
		
		List<Driverstab> driversList=null;
		DriverstabDAO driverstabDAO=new DriverstabDAO();
		driverstabDAO.getSession().beginTransaction();
		try
		{
			
			String hql="from Driverstab as d where d.account like :account";
			Query result=driverstabDAO.getSession().createQuery(hql);
			
			
			System.out.println("get account  "+reciveAccount+"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
			result.setString("account",reciveAccount);
			driversList=result.list();
		}
		catch(HibernateException e)
		{
			System.out.println("hibernate error in LoginServlet");
			e.printStackTrace();
		}
		finally
		{
			driverstabDAO.getSession().getTransaction().commit();
			driverstabDAO.getSession().close();
			
		}
		JSONObject resultJson=null;
		if(driversList.size()>=1)
		{
			Driverstab theDriver= driversList.get(0);
			if(theDriver.getPwd().equals(recivePwd))
			{
				resultJson =new JSONObject();
				try {
					
					request.getSession(true).setAttribute("userID", Integer.toString(driversList.get(0).getUuid()));
					resultJson.put("result","succeed");
					resultJson.put("account", theDriver.getAccount());
					resultJson.put("name", theDriver.getName());
					resultJson.put("telNumberd", theDriver.getTelNumber());
					resultJson.put("licence",theDriver.getLicence());
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
		if(resultJson==null)
		{
			resultJson =new JSONObject();
			try {
				resultJson.put("result","fail");
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		OutputStream out=response.getOutputStream();
		
//		for (Driverstab driverstab : driversList) {
//			out.write(driverstab.getAccount().toString().getBytes("utf-8"));
//			out.write("\n".getBytes("utf-8"));
//			out.write(driverstab.getPwd().toString().getBytes("utf-8"));
//			
//		}
		out.write(resultJson.toString().getBytes("utf-8"));
		out.flush();
		out.close();
		System.out.println(resultJson.toString()+"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
	}

}
